package cn.xzqwjw.taskmanager.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 加密解密要调用的一些方法
 *
 * @author rush
 */
@Slf4j
public class CryptoUtil {

  public static void test(String strIn) {
    // 随机生成密钥
    byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
    // 构建
    SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
    // 加密
    byte[] encrypt = aes.encrypt(strIn);
    // 解密
    byte[] decrypt = aes.decrypt(encrypt);

    System.out.println("输入字符串为:" + strIn);
    // 加密为16进制表示
    String encryptHex = aes.encryptHex(strIn);
    System.out.println("加密后的64位长度字符串为:" + encryptHex);
    // 解密为字符串
    String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
    System.out.println("解密字符串为:" + decryptStr);
  }

  public static PasswordEncoder initEncoder() {
    return new BCryptPasswordEncoder();
  }

}
